package com.qzdatasoft.mcp.server.demo.rest;

import com.qzdatasoft.mcp.server.demo.entity.User;
import com.qzdatasoft.mcp.server.demo.repository.UserRepository;
import com.qzdatasoft.mcp.server.common.IMcpToolProvider;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class UserController  implements IMcpToolProvider {

    private final UserRepository userRepository;

    public UserController(UserRepository userRepository ){
        this.userRepository = userRepository;
    }

    /**
     * Get all users
     *
     * @return list of users
     */
    @GetMapping("/users")
    @Tool(description = "Get all users")
    public List<User> getUsers() {
        return userRepository.getUsers();
    }

    /**
     * Add a new user
     *
     * @param user user to add
     * @return success message
     */
    @PostMapping("/users")
    @Tool(description = "Add a new user")
    public User addUser(@ToolParam(description = "user to add") @RequestBody User user) {
        return userRepository.add(user);
    }


    @GetMapping("/users")
    @Tool(description = "Get user info by id")
    public User getUserInfo(@ToolParam(description = "user id") int id) {
        return userRepository.getUsers().stream().filter(user -> user.getId() == id).findFirst().orElse(null);
    }
}
